/**
 *
 * @author ilya portnyagin iportnyagin@gmail.com
 */

package ru.portnyagin.helpdeskru.controller.admin;

import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import ru.portnyagin.helpdeskru.model.ServiceObject;
import ru.portnyagin.helpdeskru.service.ServiceObjectService;
import ru.portnyagin.helpdeskru.util.JsfUtil;

@ManagedBean(name = "serviceObjectController")
@ViewScoped
public class AdminServiceObjectController implements Serializable {

    @EJB
    private ru.portnyagin.helpdeskru.service.ServiceObjectService serviceObjectService;

    private List<ServiceObject> listAllServiceObject = null;
    private ServiceObject currentServiceObject = null;
            
    public AdminServiceObjectController() {
    }

    private ServiceObjectService getServiceObjectService() {
        return serviceObjectService;
    }

    public List<ServiceObject> getListAllServiceObject() {
        if(listAllServiceObject == null) {
            listAllServiceObject = getServiceObjectService().findAll();
        }
        return listAllServiceObject;
    }
    
    public SelectItem[] getSelectItemListServiceObject() {
        return JsfUtil.getSelectItems(this.getListAllServiceObject(), true);
    }

    public ServiceObject getCurrentServiceObject() {
        return currentServiceObject;
    }

    public void setCurrentServiceObject(ServiceObject currentServiceObject) {
        this.currentServiceObject = currentServiceObject;
    }
    
    
    public void addNewServiceObject() {
        listAllServiceObject.add(0, new ServiceObject());
    }
    
    public void deleteServiceObject() {
        getServiceObjectService().remove(currentServiceObject);
    }
    
    public void saveAll() {
        try{
            for(ServiceObject s: listAllServiceObject) {
                if(s.getId() != null) {
                    getServiceObjectService().edit(s);
                } else {
                    getServiceObjectService().create(s);
                }
            }
        } catch (Exception e){
            JsfUtil.addErrorMessage(e.toString());
        }        
    }
    
    
}
